<?php

namespace App\Miya\Middlewares;

use Closure;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class CacheCheck
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->check()) {
            return $next($request);
        } else {
            Log::alert(trans('miya.cache_check'));
            abort(500);
        }
    }

    protected function check()
    {
        return $this->checkFalsify() && $this->checkOMS();
    }

    /**
     * 防窜
     * @return bool
     */
    protected function checkFalsify()
    {
        return Cache::has('falsify_check');
    }

    /**
     * OMS通讯
     * @return bool
     */
    protected function checkOMS()
    {
        return Cache::has('oms_user') &&
            Cache::has('oms_order') &&
            Cache::has('oms_black_list') &&
            Cache::has('allow_check');
    }
}
